package cn.edu.cqytxy;

import cn.edu.cqytxy.entity.Cat;
import cn.edu.cqytxy.entity.CoffeeStore;
import cn.edu.cqytxy.entity.Dog;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 演示泛型与集合
 *
 * @author yinjw
 */
public class Test {

    public static void main(String[] args) {

//        Cat cat1 = new Cat("小白", 2, "白色");
//        Cat cat2 = new Cat("小黑", 3, "黑色");
//
//        Cat[] cats = new Cat[2];
//        cats[0] = cat1;
//        cats[1] = cat2;
//
//        for (Cat cat : cats) {
//            System.out.println(cat);
//        }
//
//        // 调换数组元素的位置
//        to(cats);
//
//        for (Cat cat : cats) {
//            System.out.println(cat);
//        }

        Dog dog1 = new Dog("小白", 2, "白色");
        Dog dog2 = new Dog("小黑", 3, "黑色");

        Dog[] dogs = new Dog[2];
        dogs[0] = dog1;
        dogs[1] = dog2;

        for (Dog dog : dogs) {
            System.out.println(dog);
        }

        // 调换数组元素的位置
        to(dogs);

        for (Dog dog : dogs) {
            System.out.println(dog);
        }
    }


    /**
     * 对象调换位置
     */
    public static <T> void to(T[] cats) {
        T cat = cats[0];
        cats[0] = cats[1];
        cats[1] = cat;
    }

    private static void test4() {
        Dog[] dogs = new Dog[3];
        dogs[0] = new Dog("大黄", 2, "黄色");
        dogs[1] = new Dog("大黑", 1, "黑色");
        dogs[2] = new Dog("大白", 3, "白色");

        for (Dog dog : dogs){
            System.out.println(dog);
        }

        Arrays.sort(dogs, new Comparator<Dog>() {
            @Override
            public int compare(Dog o1, Dog o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        for (Dog dog : dogs){
            System.out.println(dog);
        }
    }

    private static void test3() {
        Cat[] cats = new Cat[3];
        cats[0] = new Cat("小白", 2, "白色");
        cats[1] = new Cat("小黑", 1, "黑色");
        cats[2] = new Cat("小黄", 3, "黄色");

        for (Cat cat : cats) {
            System.out.println(cat);
        }

        Arrays.sort(cats);

        for (Cat cat : cats) {
            System.out.println(cat);
        }
    }

    private static void test2() {

        Dog dog = new Dog();
        dog.setName("大黄");
        dog.setAge(2);
        dog.setColor("黄色");
        CoffeeStore<Dog> coffeeStore = new CoffeeStore<>();
        coffeeStore.setObject(dog);

        Dog object = coffeeStore.getObject();
        System.out.println(object);
    }


    private static void test1() {
        Cat cat = new Cat();
        cat.setColor("白色");
        cat.setName("大白");
        cat.setAge(3);

        Dog dog = new Dog();
        dog.setName("大黄");
        dog.setAge(2);
        dog.setColor("黄色");

        CoffeeStore coffeeStore = new CoffeeStore();
        coffeeStore.setName("小猫咖啡");
        coffeeStore.setObject(dog);

        System.out.println(coffeeStore.getObject());

        Object object = coffeeStore.getObject();
        Cat cat1 = (Cat) object;
        cat1.catchMouse();
    }
}
